package com.rental.quickenPoint.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rental.common.result.AjaxResult;
import com.rental.common.result.ResultCodeEnum;
import com.rental.model.entity.chargeRecord.ChargeRecord;
import com.rental.model.entity.UserAccount;
import com.rental.model.entity.user.UserInfo;
import com.rental.quickenPoint.mapper.ChargeRecordMapper;
import com.rental.quickenPoint.mapper.UserAccountMapper;
import com.rental.quickenPoint.service.IChargeRecordService;
import com.rental.user.client.UserInfoFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hjk
 * @since 2025-01-16
 */
@Service
public class ChargeRecordServiceImpl extends ServiceImpl<ChargeRecordMapper, ChargeRecord> implements IChargeRecordService {
    @Autowired
    private ChargeRecordMapper chargeRecordMapper;

    @Autowired
    private UserAccountMapper userAccountMapper;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;



    @Override
    public Boolean advertising(ChargeRecord chargeRecord,Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid, userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        if (userAccount == null) {
            return false;
        }
        if (userAccount.getQuickenPoint() < 800) {
            //提示加速值余额不足
            return false;
        }
        //扣减加速值
        userAccount.setQuickenPoint(userAccount.getQuickenPoint() - 800);
        userAccountMapper.updateById(userAccount);
        //插入加速记录
        chargeRecord.setUid(userId);
        chargeRecord.setChangedValue("Id为"+userId+"消耗加速值"+800);
        chargeRecord.setCreateTime(new Date());
        chargeRecord.setActivity("公众号推广");
        chargeRecord.setIsDelete(0);
        chargeRecord.setUpdateTime(new Date());
        chargeRecordMapper.insert(chargeRecord);
        return true;
    }

    @Override
    public Boolean sign(Long userId) {
        try {
            // 先判断今天是否已经签到过
            if (hasSignedToday(userId)) {
                return false;
            }
            // 判断该用户是否在账户中
            UserAccount userAccount = getUserAccount(userId);
            if (userAccount == null) {
                return false;
            }
            // 修改账户信息
            updateUserAccount(userAccount);
            // 插入加速记录
            insertChargeRecord(userId);
            return true;
        } catch (Exception e) {
            // 处理异常，这里简单打印日志，实际项目中可以根据需求进行处理
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 判断用户今天是否已经签到
     * @param userId 用户ID
     * @return 是否已经签到
     */
    private boolean hasSignedToday(Long userId) {
        LocalDate today = LocalDate.now();
        LambdaQueryWrapper<ChargeRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChargeRecord::getUid, userId)
                .apply("DATE(create_time) = {0}", today);
        List<ChargeRecord> list = chargeRecordMapper.selectList(lambdaQueryWrapper);
        for (ChargeRecord chargeRecord : list) {
            if (chargeRecord.getChangedValue().equals("Id为" + userId + "的用户签到获得加速值" + 10)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 根据用户ID获取用户账户信息
     * @param userId 用户ID
     * @return 用户账户信息
     */
    private UserAccount getUserAccount(Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid, userId);
        return userAccountMapper.selectOne(lambdaQueryWrapper);
    }
    /**
     * 更新用户账户信息
     * @param userAccount 用户账户信息
     */
    private void updateUserAccount(UserAccount userAccount) {
        userAccount.setQuickenPoint(userAccount.getQuickenPoint() + 10);
        userAccount.setUpdateTime(new Date());
        userAccountMapper.updateById(userAccount);
    }
    /**
     * 插入加速记录
     * @param userId 用户ID
     */
    private void insertChargeRecord(Long userId) {
        ChargeRecord chargeRecord = new ChargeRecord();
        chargeRecord.setCreateTime(new Date());
        chargeRecord.setUpdateTime(new Date());
        chargeRecord.setUid(userId);
        chargeRecord.setActivity("每日签到");
        chargeRecord.setChangedValue("Id为" + userId + "的用户签到获得加速值" + 10);
        chargeRecordMapper.insert(chargeRecord);
    }

    @Override
    public List<ChargeRecord> getChargeRecordInfo(Long userId) {
        LambdaQueryWrapper<ChargeRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ChargeRecord::getUid,userId);
        List<ChargeRecord> list = chargeRecordMapper.selectList(lambdaQueryWrapper);
        //设置加速值明示信息
        return list;
    }

    @Override
    public String toRegister(Long userId,String idCard,String realName) {
        //判断当前用户是否已经实名认证
        UserInfo userInfo = userInfoFeignClient.getUserInfoById(userId).getData();
        if(userInfo == null){
            return ResultCodeEnum.NO_USER.getMessage();
        }
        if(userInfo.getIdCard() != null){
            return ResultCodeEnum.In_REAL_NAME.getMessage();
        }
        userInfo.setIdCard(idCard);
        userInfo.setRealName(realName);
        userInfo.setUpdateTime(new Date());
        userInfoFeignClient.updateUserInfoById(userInfo);
        //修改加速值
        UserAccount userAccount = userAccountMapper.selectById(userId);
        if(userAccount == null){
            return ResultCodeEnum.NO_USER_ACCOUNT.getMessage();
        }
        userAccount.setQuickenPoint(userAccount.getQuickenPoint()+10);
        userAccountMapper.updateById(userAccount);
        return ResultCodeEnum.REAL_NAME_OK.getMessage();
    }

    @Override
    public Boolean connectWay(Long userId) {
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid, userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        if (userAccount == null) {
            return false;
        }
        if (userAccount.getQuickenPoint() < 10) {
            //提示加速值余额不足
            return false;
        }
        //扣减加速值
        userAccount.setQuickenPoint(userAccount.getQuickenPoint() - 10);
        userAccountMapper.updateById(userAccount);
        ChargeRecord chargeRecord = new ChargeRecord();
        //插入加速记录
        chargeRecord.setUid(userId);
        chargeRecord.setChangedValue("Id为"+userId+"消耗加速值"+10);
        chargeRecord.setCreateTime(new Date());
        chargeRecord.setActivity("直连加速");
        chargeRecord.setIsDelete(0);
        chargeRecord.setUpdateTime(new Date());
        chargeRecordMapper.insert(chargeRecord);
        return true;
    }

    @Override
    public Boolean advertisingWeiBo(ChargeRecord chargeRecord,Long userId) {
        //获取当前登录人Id
        LambdaQueryWrapper<UserAccount> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAccount::getUid, userId);
        UserAccount userAccount = userAccountMapper.selectOne(lambdaQueryWrapper);
        if (userAccount == null) {
            return false;
        }
        if (userAccount.getQuickenPoint() < 1500) {
            //提示加速值余额不足
            return false;
        }
        //扣减加速值
        userAccount.setQuickenPoint(userAccount.getQuickenPoint() - 1500);
        userAccountMapper.updateById(userAccount);
        //插入加速记录
        chargeRecord.setUid(userId);
        chargeRecord.setActivity("微博推广");
        chargeRecord.setCreateTime(new Date());
        chargeRecord.setUpdateTime(new Date());
        chargeRecord.setIsDelete(0);
        chargeRecord.setChangedValue("Id为"+userId+"消耗加速值"+1500);
        chargeRecordMapper.insert(chargeRecord);
        return true;
    }
}
